//
//  OADTConfigAPIViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 4/4/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTConfigAPIViewController.h"
#import "OABase.h"
#import "OATableConfigHeader.h"
#import "OADefaultCell.h"
#import "OADMocker.h"
#import "OAActionCell.h"

@interface OADTConfigAPIViewController ()

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tableHeaderView;
@property (nonatomic, strong) UIView *tableFooterView;

@end

@implementation OADTConfigAPIViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
//    self.tableView.tableHeaderView = self.tableHeaderView;
//    self.tableView.tableFooterView = self.tableFooterView;
    
    [self.view addSubview:self.tableView];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    
//    __weak typeof(self) weakSelf = self;
    
    for (NSInteger i = 0; i < 1; i++) {
        OADefaultCellViewModel *cellViewModel = [OADefaultCellViewModel new];
        cellViewModel.title = [OADMocker mockTitle];
        cellViewModel.content = [OADMocker mockContent];
        OACellConfig *cellConfig =
        [self.tableView.oa_tableConfig addCellWithName:nil type:nil viewModel:cellViewModel];
        
        cellConfig.cellDidSelect = ^(OACellConfig *cellConfig) {
            NSLog(@"0 section is: %ld", (long)cellConfig.indexPath.section);
        };
        
    }
    
//    OASectionConfig *section_1 = [self.tableView.oa_tableConfig sectionAtIndex:0];
//    for (NSInteger i = 0; i < 1; i++) {
//        OADefaultCellViewModel *cellViewModel = [OADefaultCellViewModel new];
//        cellViewModel.title = [OADMocker mockTitle];
//        cellViewModel.content = [OADMocker mockContent];
//
//        OACellConfig *cellConfig =
//        [section_1 addCellWithName:nil type:nil viewModel:cellViewModel];
//
//        cellConfig.cellDidSelect = ^(OADefaultCellViewModel *viewModel, NSIndexPath *indexPath) {
//            NSLog(@"1 section: %ld, row: %ld", (long)indexPath.section, (long)indexPath.row);
//        };
//    }
    
    //
    OASectionConfig *s1 = [self.tableView.oa_tableConfig addSectionWithName:nil];
    [s1 addCell:[self actionCellConfig]];
    
    //
    OASectionConfig *section_2 = [self.tableView.oa_tableConfig addSectionWithName:@"s2"];
    for (NSInteger i = 0; i < 12; i++) {
        OADefaultCellViewModel *cellViewModel = [OADefaultCellViewModel new];
        cellViewModel.title = [OADMocker mockTitle];
        cellViewModel.content = [OADMocker mockContent];
        
        OACellConfig *cellConfig = [section_2 addCellWithName:nil type:nil viewModel:cellViewModel];
//        cellConfig.topSeparator = (OALineInset){.bottom = 20, .height = 5, .left = 15};
        cellConfig.topSeparatorColor = OARandomColor();
//        cellConfig.bottomSeparator = (OALineInset){.top = 20, .height = 50, .bottom = 20, .left = 15, .right = 15,};
        cellConfig.bottomSeparatorColor = OAColor(cdcdcd);
        
        cellConfig.cellDidSelect = ^(OACellConfig *cellConfig) {
            NSLog(@"2 section: %ld, row: %ld", (long)cellConfig.indexPath.section, (long)cellConfig.indexPath.row);
        };
    }
    
    OACellConfig *cell_1 = [section_2 cellAtIndex:0];
    NSLog(@"cellName is: %@", cell_1.name);
    
    OADefaultCellViewModel *vm_1 = [section_2 viewModelAtIndex:0];
    NSLog(@"title is: %@", vm_1.title);
    NSLog(@"content is: %@", vm_1.content);
    
    OADefaultCellViewModel *cell_1_vm = cell_1.cellViewModel;
    cell_1_vm.title = [NSString stringWithFormat:@"s:%ld,r:%ld",
                       cell_1.indexPath.section,
                       cell_1.indexPath.row];
    
    vm_1.content = @"New Content";
    
//    for (OASectionConfig *section in self.tableView.oa_tableConfig.sections) {
//        NSLog(@"section: %@", section.name);
//
//        for (OACellConfig *cell in section.cells) {
//            NSLog(@"cell: %@", cell.name);
//        }
//    }
    
}

- (OACellConfig *)actionCellConfig {
    
    __weak typeof(self) weakSelf = self;
    
    OAActionCellViewModel *cellViewModel = [self actionCellViewModel];
    
    OACellConfig *cellConfig = [OACellConfig cellWithName:nil
                                                 cellType:@"OAActionCell"
                                                viewModel:cellViewModel];
    
    cellConfig.cellDidSelect = ^(OACellConfig *cellConfig) {
        NSLog(@"s:%ld, r:%ld", (long)cellConfig.indexPath.section, (long)cellConfig.indexPath.row);
    };
    
    
    //
    [cellConfig setAction:^(OACellConfig *cellConfig) {
//        OASectionConfig *section = [weakSelf.tableView.oa_tableConfig sectionWithCellConfig:cellConfig];
        
//        OASectionConfig *section = cellConfig.sectionConfig;
//        [section insertCellConfig:[weakSelf actionCellConfig] beforeCellConfig:weakCellConfig];
        
//        [section insertCellConfig:[weakSelf actionCellConfig] atIndex:10];
        
        [cellConfig.sectionConfig addCell:[weakSelf actionCellConfig]];
        
        [weakSelf.tableView reloadData];
        
    } forEvent:@(OAActionCellEventAddBeforeButtonDidClick)];
    
    //
    [cellConfig setAction:^(OACellConfig *cellConfig) {
        
        OASectionConfig *section = [weakSelf.tableView.oa_tableConfig sectionAtIndex:cellConfig.indexPath.section];
        [section insertCell:[weakSelf actionCellConfig] afterCell:cellConfig];
        [weakSelf.tableView reloadData];
        
    } forEvent:@(OAActionCellEventAddAfterButtonDidClick)];
    
    //
    [cellConfig setAction:^(OACellConfig *cellConfig) {
        
        OASectionConfig *section = [weakSelf.tableView.oa_tableConfig sectionAtIndex:cellConfig.indexPath.section];
        [section deleteCellBeforeCell:cellConfig];
        [weakSelf.tableView reloadData];
        
    } forEvent:@(OAActionCellEventDeleteBeforeButtonDidClick)];
    
    //
    [cellConfig setAction:^(OACellConfig *cellConfig) {
        
        OASectionConfig *section = [weakSelf.tableView.oa_tableConfig sectionAtIndex:cellConfig.indexPath.section];
        [section deleteCell:cellConfig];
        [weakSelf.tableView reloadData];
        
    } forEvent:@(OAActionCellEventDeleteButtonDidClick)];
    
    //
    [cellConfig setAction:^(OACellConfig *cellConfig) {
        
        OASectionConfig *section = [weakSelf.tableView.oa_tableConfig sectionAtIndex:cellConfig.indexPath.section];
        [section deleteCellAfterCell:cellConfig];
        [weakSelf.tableView reloadData];
        
    } forEvent:@(OAActionCellEventDeleteAfterButtonDidClick)];
    
    
    return cellConfig;
}

- (OAActionCellViewModel *)actionCellViewModel {
    OAActionCellViewModel *viewModel = [OAActionCellViewModel new];
    viewModel.title = [OADMocker mockTitle];
    viewModel.content = [OADMocker mockContent];
    
//    __weak typeof(self) weakSelf = self;
    
//    viewModel.addBeforeButtonDidClick = ^(NSIndexPath *indexPath, OAActionCellViewModel *viewModel) {
//        OAActionCellViewModel *cellViewModel = [weakSelf actionCellViewModel];
//        [weakSelf.tableView.oa_tableConfig addCellWithName:nil
//                                                      type:@"OAActionCell"
//                                                 viewModel:cellViewModel];
//        [weakSelf.tableView reloadData];
//    };
    
//    viewModel.addAfterButtonDidClick = ^{
//        NSLog(@"%@", NSStringFromSelector(_cmd));
//    };
//
//    viewModel.deleteBeforeButtonDidClick = ^{
//        NSLog(@"%@", NSStringFromSelector(_cmd));
//    };
//
//    viewModel.deleteButtonDidClick = ^{
//        NSLog(@"%@", NSStringFromSelector(_cmd));
//    };
//
//    viewModel.deleteAfterButtonDidClick = ^{
//        NSLog(@"%@", NSStringFromSelector(_cmd));
//    };
    
    return viewModel;
}



#pragma mark - Event response methods



#pragma mark - Deletate methods



#pragma mark - Setter



#pragma mark - Getter

OAGetGroupedTableView(tableView, ^(UITableView *tableView){
    
})

OAGetView(tableHeaderView, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    view.frame = CGRectMake(0, 0, 0, 30);
})

OAGetView(tableFooterView, ^(UIView *view){
    view.backgroundColor = OARandomColor();
    view.frame = CGRectMake(0, 0, 0, 30);
})

#pragma mark - Memory management

- (void)dealloc {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end
